function Get-ScriptDirectory
{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    Split-Path $Invocation.MyCommand.Path
}

function Get-RandomString
{
    $rand = New-Object System.Random
    1..20 | ForEach { $str = $str + [char]$rand.next(64,91) }
    return $str
}

cd (Get-ScriptDirectory)

$rev = "dev"
if ($args.Length -eq 1) {$rev = $args[0]}
$targetfilename = "3DTicTacToe-$rev-Source.zip"

$temppath = [System.IO.Path]::GetTempPath()
$randstr = (Get-RandomString)
$randpath = Join-Path $temppath $randstr
$targetpath = Join-Path $randpath "3DTicTacToe"

echo "Creating binary archive: $targetfilename"
echo "================================================================================"

if (Test-Path $targetfilename)
{
    echo "Removing old archive..."
    Remove-Item $targetfilename
}

New-Item -type directory -path $randpath
New-Item -type directory -path $targetpath

Copy-Item -recurse "..\*" $targetpath 
Get-ChildItem $randpath -recurse -force | Where { $_.Name -Match "^obj$|^bin$|.svn" } | Remove-Item -force -recurse

.\7za a -r -tzip $targetfilename $targetpath
.\7za t $targetfilename

echo "================================================================================"

echo "Cleaning up..."
Remove-Item -recurse $randpath 
echo "done"

echo "================================================================================"